﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Biz;

namespace MockPrjTeam2.Pages.Views
{
    public partial class PopupSearchAddress1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //btSelect.Attributes.Add("onclick", "javascript:return Validate()");
                ShowAllAddress();
            }
        }
        private void ShowAllAddress()
        {
            var listAddr = new List<Address>();
            var getAddr = new AddressBiz();
            listAddr = getAddr.getAllAddress();

            GVSearchAddress.DataSource = listAddr;
            GVSearchAddress.DataBind();
        }        
        protected void btNone_Click(object sender, EventArgs e)
        {
            ShowAllAddress();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var listAddr = new List<Address>();
            var getAddr = new AddressBiz();
            listAddr = getAddr.getAddressesByKey(Convert.ToString(txtPostCode.Text), Convert.ToString(txtStreet.Text), Convert.ToString(txtTown.Text));

            if (listAddr != null)
            {
                GVSearchAddress.DataSource = listAddr;
                GVSearchAddress.DataBind();
            }
        }       
        protected void GVSearchAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVSearchAddress.PageIndex = e.NewPageIndex;
            GVSearchAddress.DataBind();            
        }

        protected void btSelect_Click(object sender, EventArgs e)
        {
            string selectedValue = Request.Form["MyRadioButton"];
            var add = new Address();
            var getbyId = new AddressBiz();
            add = getbyId.getbyId(Convert.ToInt32(selectedValue));
          //  txtAddressId.Text = Convert.ToString(add.AddressId);
            txtPostCode.Text = Convert.ToString(add.PostCode);
            hdnAddressId.Value =Convert.ToString(add.AddressId);
            Session["Address"] = txtPostCode.Text;
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");

            script.AppendFormat("window.opener.document.getElementById('hdnAddressId').value = '{0}';", hdnAddressId.Value);
            script.AppendFormat("window.opener.document.getElementById('txtPostCode').value = '{0}';",txtPostCode.Text);
            
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void GVSearchAddress_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }

        protected void GVSearchAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
              
    }
}